<template>
    <view>
        <view class="goods-list">
            <view v-for="(goods , i) in goodsList" :key="i" @click="gotoDetail(goods)">
                <my-goods :goods="goods"></my-goods>
            </view>
        </view>
    </view>
</template>

<script>
    export default {
        data() {
            return {
                queryObj: {
                    // 查询关键词
                    query: '',
                    // 分类id
                    cid: '',
                    // 页数索引
                    pagenum: 1,
                    // 每页长度
                    pagesize: 10,
                },
                total: 0,
                goodsList: [],

            }
        },
        onLoad(options) {
            this.queryObj.query = options.query || ''
            this.queryObj.cid = options.cid || ''
            this.getGoodsList()
        },
        methods: {
            // 获取商品列表事件
            async getGoodsList(callback){
                const res = await uni.$http.get('/api/public/v1/goods/search', this.queryObj)
                callback && callback()
                if (res.data.meta.status !== 200) return uni.$showMsg()
                this.goodsList = [...this.goodsList, ...res.data.message.goods]
                this.total = res.data.message.total
            },
            // 跳转到商品详情页事件
            gotoDetail(goods){
                uni.navigateTo({
                    url:`/subpackage/goods_detail/goods_detail?goods_id=${goods.goods_id}`
                })
            }
        },
        // 上拉触底事件
        onReachBottom() {
            if (this.queryObj.pagenum * this.queryObj.pagesize >= this.total) {
                return uni.showToast({
                    title:'已经到底了！',
                    image:'/static/none.gif',
                    duration:3000
                })
            }
            this.queryObj.pagenum++
            this.getGoodsList()
        },
        // 下拉刷新事件
        onPullDownRefresh() {
            this.queryObj.pagenum = 1
            this.goodsList = []
            this.total = 0
            this.getGoodsList(() => uni.stopPullDownRefresh())
        },
    }
</script>

<style lang="scss">

</style>
